<?PHP
	session_start();
	session_cache_expire(30);
?>
<!--
	calendar.php
	@author Max Palmer and Allen Tucker
	@version 3/26/08, revised 9/15/2008
-->
<html>
	<head>
		<title>
			Calendar display and edit form
		</title>
		<link rel="stylesheet" href="styles.css" type="text/css" />
		<link rel="stylesheet" href="calendar.css" type="text/css" />
	</head>
	<body>
		<div id="container">
			<?PHP include('header.php');?>
			<div id="content">
				<span class="viewweek">
				<?php
					include_once('database/dbWeeks.php');
					include_once('database/dbPersons.php');
					include 'calendar.inc';
					// checks to see if in edit mode
					$edit=$_GET['edit'];
					if($edit!="true")
						$edit=false;
					else
						$edit=true;
					// gets the week to show, if no week then defaults to current week
					$weekid=$_GET['id'];
					if(!$weekid)
						$weekid=date("m-d-y",time());
					$week=get_dbWeeks($weekid);
					// if invalid week or unpublished week and not a manager
					if(! $week instanceof Week)
						echo "<p>Invalid week id or week does not exist.</p>";
					else if($week->get_status()=="unpublished" && $_SESSION['access_level']<2)
						echo "<p>Invalid week id or week does not exist.</p>";
					else {
						// if notes were edited, processes notes
						if(array_key_exists('_submit_check_edit_notes', $_POST) && $_SESSION['access_level']>=2) {
							process_edit_notes($week,$_POST);
							$week=get_dbWeeks($weekid);
						}
						// shows the previous week / next week navigation
						$week_nav=do_week_nav($week,$edit,"Fam");
						echo $week_nav;
						// prevents archived weeks from being edited by anyone
						if($week->get_status()=="archived")
							$edit=false;
						echo '<form method="POST">';
						    $days=$week->get_dates();
							$year=date("Y",time());
							$doy=date("z",time())+1;
						//    show_week($days,$week,$edit,$year,$doy);
						//    echo('<br><br>');
						    show_fam_week($days,$week,$edit,$year,$doy);
						    echo "<input type=\"hidden\" name=\"_submit_check_edit_notes\" value=\"1\">";
							echo "<input type=\"hidden\" name=\"weekid\" value=\"".$days[0]->get_id()."\">";
						    if ($edit==true && !($days[6]->get_year()<$year || ($days[6]->get_year()==$year && $days[6]->get_day_of_year()<$doy) ) && $_SESSION['access_level']>=2)
								echo "<p align=\"center\"><input type=\"submit\" value=\"Save changes to all notes\" name=\"submit\">";
						echo '</form>';
						// echo $week_nav;

					}
				?>
				<?PHP include('footer.inc');?>
			</div>
		</div>
	</body>
</html>

<?php

	// display a week on the family room calendar
	function show_fam_week($days,$week,$edit) {
		// gets all of the dates for this week
		$daynames = array ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday");
		// sets up the table, with title, and then day of month
		echo ('<table id="calendar" align="center" ><form method="POST"><tr class="weekname"><td colspan="15" ' .
				'align="center" >'.'Family Room Calendar: '.$week->get_name().'</td></tr>
				<tr><td bgcolor="#7991B1"></td>');
		for($i=0; $i<5; $i++)
			echo ('<td class="dom">'.get_dom($days[$i]).'</td><td class="dow">&nbsp; '. $daynames[$i] .'</td>');
		echo('</tr>');
	// 10-1
		echo ("<tr><td class=\"hour\">10am</td>");
		for ($i=0; $i<5; $i++)
		   echo (do_shift($days[$i],"9-12Fam",3,$edit));
		echo("<tr><td class=\"hour\">11am</td>");
		echo("<tr><td class=\"hour\">12pm</td>");
		echo ("</tr>");

	// 1-4
		echo ("<tr><td class=\"hour\">1pm</td>");
		for ($i=0; $i<5; $i++)
		   echo (do_shift($days[$i],"12-3Fam",3,$edit));
		echo("<tr><td class=\"hour\">2pm</td>");
		echo("<tr><td class=\"hour\">3pm</td>");
		echo ("</tr>");
	// 4-7
		echo ("</tr><tr><td class=\"hour\">4pm</td>");
		for ($i=0; $i<5; $i++)
		   echo (do_shift($days[$i],"3-6Fam",3,$edit));
		echo("<tr><td class=\"hour\">5pm</td>");
		echo("<tr><td class=\"hour\">6pm</td>");
		echo("</tr>");
		echo "</table>";
	}

	?>